<template>
  <div class="industry-domain-container">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>行业领域管理</span>
          <el-button type="primary" @click="handleAdd">新增行业领域</el-button>
        </div>
      </template>

      <!-- 搜索表单 -->
      <el-form :model="searchForm" ref="searchFormRef" :inline="true" class="search-form">
        <el-form-item label="领域ID" prop="industryDomainId">
          <el-input v-model="searchForm.industryDomainId" placeholder="请输入领域ID" clearable />
        </el-form-item>
        <el-form-item label="领域名称" prop="industryDomainName">
          <el-input v-model="searchForm.industryDomainName" placeholder="请输入领域名称" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 数据表格 -->
      <el-table
        v-loading="loading"
        :data="industryDomainList"
        border
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="industryDomainId" label="领域ID" width="100" />
        <el-table-column prop="industryDomainName" label="领域名称" />
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column prop="updateTime" label="更新时间" width="180" />
        <el-table-column label="操作" width="180" fixed="right">
          <template #default="scope">
            <el-button type="primary" link @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="danger" link @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 新增/编辑对话框 -->
    <el-dialog
      :title="dialogType === 'add' ? '新增行业领域' : '编辑行业领域'"
      v-model="dialogVisible"
      width="500px"
    >
      <el-form
        :model="industryDomainForm"
        :rules="rules"
        ref="formRef"
        label-width="100px"
      >
        <el-form-item label="领域ID" prop="industryDomainId">
          <el-input v-model="industryDomainForm.industryDomainId" placeholder="请输入领域ID" />
        </el-form-item>
        <el-form-item label="领域名称" prop="industryDomainName">
          <el-input v-model="industryDomainForm.industryDomainName" placeholder="请输入领域名称" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { 
  getIndustryDomainPage, 
  addIndustryDomain, 
  updateIndustryDomain, 
  deleteIndustryDomain 
} from '../../api/industryDomain'

// 搜索表单
const searchForm = reactive({
  industryDomainId: '',
  industryDomainName: ''
})

// 表单引用
const searchFormRef = ref(null)
const formRef = ref(null)

// 表格数据
const loading = ref(false)
const industryDomainList = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 对话框相关
const dialogVisible = ref(false)
const dialogType = ref('add') // 'add' 或 'edit'
const industryDomainForm = reactive({
  id: null,
  industryDomainId: null,
  industryDomainName: ''
})

// 表单验证规则
const rules = {
  industryDomainId: [
    { required: true, message: '请输入领域ID', trigger: 'blur' },
    { type: 'number', message: '领域ID必须为数字', trigger: 'blur', transform: value => Number(value) }
  ],
  industryDomainName: [
    { required: true, message: '请输入领域名称', trigger: 'blur' },
    { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
  ]
}

// 获取行业领域列表
const getIndustryDomainList = async () => {
  loading.value = true
  try {
    const params = {
      current: currentPage.value,
      size: pageSize.value,
      industryDomainId: searchForm.industryDomainId || undefined,
      industryDomainName: searchForm.industryDomainName || undefined
    }

    const res = await getIndustryDomainPage(params)
    if (res.code === 200) {
      industryDomainList.value = res.data.records
      total.value = res.data.total
    } else {
      ElMessage.error(res.message || '获取行业领域列表失败')
    }
  } catch (error) {
    console.error('获取行业领域列表失败', error)
    ElMessage.error('获取行业领域列表失败')
  } finally {
    loading.value = false
  }
}

// 处理页码变化
const handleCurrentChange = (val) => {
  currentPage.value = val
  getIndustryDomainList()
}

// 处理每页条数变化
const handleSizeChange = (val) => {
  pageSize.value = val
  currentPage.value = 1
  getIndustryDomainList()
}

// 搜索
const handleSearch = () => {
  currentPage.value = 1
  getIndustryDomainList()
}

// 重置搜索
const resetSearch = () => {
  searchFormRef.value.resetFields()
  currentPage.value = 1
  getIndustryDomainList()
}

// 重置表单
const resetForm = () => {
  industryDomainForm.id = null
  industryDomainForm.industryDomainId = null
  industryDomainForm.industryDomainName = ''
}

// 添加行业领域
const handleAdd = () => {
  dialogType.value = 'add'
  resetForm()
  dialogVisible.value = true
}

// 编辑行业领域
const handleEdit = (row) => {
  dialogType.value = 'edit'
  resetForm()
  
  // 填充表单数据
  Object.assign(industryDomainForm, row)
  dialogVisible.value = true
}

// 删除行业领域
const handleDelete = (row) => {
  ElMessageBox.confirm('确定要删除该行业领域吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      const res = await deleteIndustryDomain(row.id)
      if (res.code === 200) {
        ElMessage.success('删除成功')
        getIndustryDomainList()
      } else {
        ElMessage.error(res.message || '删除失败')
      }
    } catch (error) {
      console.error('删除行业领域失败', error)
      ElMessage.error('删除失败')
    }
  }).catch(() => {})
}

// 提交表单
const submitForm = () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      try {
        let res
        if (dialogType.value === 'add') {
          res = await addIndustryDomain(industryDomainForm)
        } else {
          res = await updateIndustryDomain(industryDomainForm)
        }
        
        if (res.code === 200) {
          ElMessage.success(dialogType.value === 'add' ? '添加成功' : '更新成功')
          dialogVisible.value = false
          getIndustryDomainList()
        } else {
          ElMessage.error(res.message || (dialogType.value === 'add' ? '添加失败' : '更新失败'))
        }
      } catch (error) {
        console.error(dialogType.value === 'add' ? '添加行业领域失败' : '更新行业领域失败', error)
        ElMessage.error(dialogType.value === 'add' ? '添加失败' : '更新失败')
      }
    }
  })
}

// 组件挂载时获取数据
onMounted(() => {
  getIndustryDomainList()
})
</script>

<style scoped>
.industry-domain-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-form {
  margin-bottom: 20px;
}

.pagination-container {
  margin-top: 20px;
  text-align: right;
}
</style>